/*
 * Copyright 2009 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.configleon.configurer;

import java.io.Serializable;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletContext;
import org.springframework.web.context.ServletContextAware;

public class WebPropertyConfigurer extends AbstractPropertyConfigurer implements ServletContextAware {

    private ServletContext servletContext;
    private List<String> applicationAttributes;
    private String configName;

    /**
     * <p>
     * Load the web application attributes in the 'application' scope. The
     * following is an example of attributes that will be pulled from the config
     * folder property file and registered as an application property.
     * </p>
     * 
     * <pre>
     * &lt;property name=&quot;applicationAttributes&quot;&gt;
     *     &lt;list&gt;
     *         &lt;value&gt;sitepath&lt;/value&gt;
     *     &lt;/list&gt;
     * &lt;/property&gt;
     * </pre>
     * 
     * @param properties The config resource properties.
     */
    @Override
    protected void postConfiguration(Properties properties) {
        if (applicationAttributes == null) {
            return;
        }

        Config config = new Config();

        for (String key : applicationAttributes) {
            String value = (String) properties.get(key);
            if (value == null || value.length() == 0) {
                if (logger.isWarnEnabled()) {
                    logger.warn("application attribute '" + key + "' has no value.");
                }
                continue;
            }

            servletContext.setAttribute(key, value);
            config.setProperty(key, value);

            if (logger.isDebugEnabled()) {
                logger.debug("application attribute: " + key + " --> " + value);
            }
        }

        String name = "config";
        if (configName != null) {
            name = configName;
        }

        servletContext.setAttribute(name, config);
    }

    public static class Config implements Serializable {

        private Properties properties = new Properties();

        public String getProperty(String key) {
            return properties.getProperty(key);
        }

        protected void setProperty(String key, String value) {
            properties.setProperty(key, value);
        }
    }

    protected List<String> getApplicationAttributes() {
        return applicationAttributes;
    }

    public void setApplicationAttributes(List<String> applicationAttributes) {
        this.applicationAttributes = applicationAttributes;
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    protected ServletContext getServletContext() {
        return servletContext;
    }

    public void setConfigName(String configName) {
        this.configName = configName;
    }
}
